package com.haixiaoke.saas.manage.dept.controller;

import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.constant.UserConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.utils.StringUtils;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.dept.domain.BizDept;
import com.haixiaoke.saas.dept.service.IBizDeptService;
import com.haixiaoke.saas.house.domain.DeptTree;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 企业端部门管理Controller
 * 
 * @author panda
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/manage/dept/dept")
public class ManageBizDeptController extends BaseController {
    @Autowired
    private IBizDeptService bizDeptService;

    /**
     * 查询企业端部门管理列表
     */
    @GetMapping("/list")
    public AjaxResult list(BizDept bizDept) {
        List<BizDept> list = bizDeptService.selectBizDeptList(bizDept);
        return success(list);
    }

    /**
     * 查询企业端部门管理数据结构
     */
    @GetMapping("/tree/list")
    public AjaxResult treeList(BizDept bizDept) {
        List<DeptTree> list = bizDeptService.buildTreeList(bizDept);
        return success(list);
    }

    /**
     * 导出企业端部门管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:dept:export')")
    @Log(title = "企业端部门管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizDept bizDept) {
        List<BizDept> list = bizDeptService.selectBizDeptList(bizDept);
        ExcelUtil<BizDept> util = new ExcelUtil<BizDept>(BizDept.class);
        util.exportExcel(response, list, "企业端部门管理数据");
    }



    /**
     * 获取企业端部门管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:dept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId) {
        return success(bizDeptService.selectBizDeptByDeptId(deptId));
    }

    /**
     * 新增企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('manage:dept:add')")
    @Log(title = "企业端部门管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizDept bizDept) {
        bizDept.setCreateBy(getUsername());
        return toAjax(bizDeptService.insertBizDept(bizDept));
    }

    /**
     * 修改企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('manage:dept:edit')")
    @Log(title = "企业端部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizDept bizDept) {

        if (StringUtils.equals(UserConstants.DEPT_DISABLE, bizDept.getStatus()) && bizDeptService.selectNormalChildrenDeptById(bizDept.getDeptId()) > 0) {
            return error("该部门包含未停用的子部门！");
        }

        bizDept.setUpdateBy(getUsername());
        return toAjax(bizDeptService.updateBizDept(bizDept));
    }

    /**
     * 删除企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('manage:dept:remove')")
    @Log(title = "企业端部门管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId) {
        if (bizDeptService.hasChildByDeptId(deptId)) {
            return warn("存在下级部门,不允许删除");
        }
        if (bizDeptService.checkDeptExistUser(deptId)) {
            return warn("部门存在用户,不允许删除");
        }
        return toAjax(bizDeptService.softDeleteBizDeptByDeptId(deptId));
    }
}
